<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用TP5助手函数可实现单字母函数M D U等,也可db::name方式,可双向兼容
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */
namespace app\admin\controller;
use app\admin\logic\OrderLogic;
use think\AjaxPage;
use think\Page;
use think\Db;
use app\common\his_info\His_comm;

class Hospital extends Base {
    public  $order_status;
    public  $pay_status;
    public  $shipping_status;
    public  $hosital_list;
    /*
     * 初始化操作
     */
    public function _initialize() {
        parent::_initialize();
        C('TOKEN_ON',false); // 关闭表单令牌验证
        $this->order_status = C('ORDER_STATUS');
        $this->pay_status = C('PAY_STATUS');
        $this->shipping_status = C('SHIPPING_STATUS');
        //   $this->$hosital_list =  M('system_menu')->where(array('id'=>$id))->find();
        // 订单 支付 发货状态
        $this->assign('order_status',$this->order_status);
        $this->assign('pay_status',$this->pay_status);
        $this->assign('shipping_status',$this->shipping_status);
    }

    /*
     *订单首页
     */
    public function index(){
        $list = M('hospital')->select();

        $this->assign('list',$list);
        return $this->fetch();
    }

    public function setzxbz(){
        $zxbz = I('post.zxbz');
        $id = I('id'); 
        if ($zxbz == 1) {
            $data['zxbz'] = 1;
            $res = M('hospital')->where('id',$id)->save($data);
            if ($res) {
                $this->ajaxReturn(array('msg'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>3));
            }
            
        }else{
            $data['zxbz'] = 0;
            $res = M('hospital')->where('id',$id)->save($data);
            if ($res) {
                $this->ajaxReturn(array('msg'=>2));
            }else{
                $this->ajaxReturn(array('msg'=>4));
            }
        }
    }

    public function _hospital(){
        $id = I('id');
        $list = M('hospital')->getField('hospital_id,hospital_name');

        if(IS_POST)
            {
                $data = I('post.');
                $brandVilidate = Loader::validate('Hospital');
                if(!$brandVilidate->batch()->check($data)){
                    $return = ['status'=>0,'msg'=>'操作失败','result'=>$brandVilidate->getError()];
                    $this->ajaxReturn($return);
                }
                if($id){
                    M("hospital")->update($data);
                }else{
                    M("hospital")->insert($data);
                }
                $this->ajaxReturn(['status'=>1,'msg'=>'操作成功','result'=>'']);
            }           
        $hospital = M('hospital')->find($id);
        $this->assign('hospital',$hospital);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function addeditHospital(){

    }

    public function addexcel(){
        // var_dump(session('admin_id'));exit;
        return $this->fetch();
    }
    public function phpexc(){
        $his_comm = new His_comm;

        $readexcel = $his_comm->phpreadExcel();
        var_dump($readexcel);
        unlink($readexcel['file_path']);
    }

    public function phpexc_att(){
        $his_comm = new His_comm;

        $readexcel = $his_comm->phpreadExcel();
        // var_dump($readexcel);exit;
        $i = 0;
        foreach ($readexcel['excel_array'] as $value) {
            $data['name'] = $value[0];
            $data['mobile_name'] = $value[1];
            $data['parent_id'] = $value[2];
            $data['parent_id_path'] = $value[3];
            $data['level'] = $value[4];
            $data['commission_rate'] = $value[5];
            $res = M('goods_category')->add($data);
            if ($res) {
                $i++;
            }
        }
        unlink($readexcel['file_path']);
        echo $i;
    }

    public function updata_path(){
        $his_comm = new His_comm;

        $readexcel = $his_comm->phpreadExcel();

        $i = 0;
        // var_dump($readexcel);exit;

        foreach ($readexcel['excel_array'] as $value) {
            $data['parent_id_path'] = $value[1]; 
            $res = M('goods_category')->where('id',$value[0])->save($data);
            $i++;
        }
        unlink($readexcel['file_path']);
        echo $i;
    }

    public function add_goods(){
        $his_comm = new His_comm;

        $readexcel = $his_comm->phpreadExcel();

        $i = 0;
        // var_dump($readexcel);exit;
        $n = 20000;
        foreach ($readexcel['excel_array'] as $value) {
            $data['cat_id'] = $value[0]; 
            $data['goods_sn'] = $value[1];
            $data['goods_name'] = $value[2];            
            $data['brand_id'] = $value[3];
            $data['store_count'] = $value[4];
            $data['shop_price'] = $value[5];
            $data['suppliers_id'] = $value[6];
            $data['goods_type'] = $value[7];
            $data['spec_type'] = $value[8];
            $res = M('goods')->save($data);
            $i++;
        }
        unlink($readexcel['file_path']);
        echo $i;
    }

    public function addsp(){
        $his_comm = new His_comm;

        $readexcel = $his_comm->phpreadExcel();

        $i = 0;
        // var_dump(I('post.typr'));exit;
        if (I('post.typr') == 'goods_category') {
            foreach ($readexcel['excel_array'] as $value) {
                $string .=$value[0];
                $string .='<br />';
                // $data['name'] = $value[0];
                // $res = M('goods_type')->add($data);
                // $i++;
            } 
        }elseif (I('post.typr') == 'spec') {
            M('spec_item')->where('spec_id',2)->delete();
            foreach ($readexcel['excel_array'] as $value) {
                //插入规格型号
                
                $data['item'] = $value[2];
                $data['spec_id'] = 2;
                $res = M('spec_item')->add($data);
                if ($res) {
                    $i++;                
                }
                /*
                    打印商品id
                 */ 
                // $id = M('goods')->where('goods_name',$value[0])->find();
                // $string = $id['goods_id'];
                // echo $string;
                // echo "<br />";               
            }
            // var_dump($readexcel);exit;
        }elseif(I('post.typr') == 'goods_spec'){
            // var_dump($readexcel['excel_array']);exit;
            foreach ($readexcel['excel_array'] as $value) {
                $data['goods_id'] = $value[0];
                $data['key'] = $value[1];
                $data['key_name'] = $value[2];
                $data['price'] = $value[3];
                $data['store_count'] = $value[4];
                $res = M('spec_goods_price')->add($data);
                if ($res) {
                    $i++;
                }
            } 
        }elseif(I('post.typr') == 'ec_spec'){
            foreach ($readexcel['excel_array'] as $value) {
                if ($value[0]) {
                    $string .='型号:';
                    $string .=trim($value[0]);
                }
                if ($value[1]) {
                    $string .= ' 规格:';
                    $string .= trim($value[1]);
                }
                $string .='<br />';
            }
        }else{
            foreach ($readexcel['excel_array'] as $value) {
                $data['suppliers_name'] = $value[0]; 
                $data['suppliers_contacts'] = $value[1];
                $data['suppliers_phone'] = $value[2];
                $res = M('suppliers')->add($data);
                $i++;
            }
        }


        // echo $string;
        // echo "<br />";
        // echo $s;
        unlink($readexcel['file_path']);
        // var_dump($readexcel['excel_array']);exit;

        echo $i;
    }
    public function outtype(){
        $list = M('goods_category')->field('name')->where('level',3)->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单编号</td>';
        $strTable .= '</tr>';
        foreach ($list as $key => $value) {
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$value['name'].'</td>';
            $strTable .= '</tr>';
        }
        $strTable .='</table>';
        downloadExcel($strTable,'order');
        exit();
    }

    public function excel_type(){
        $his_comm = new His_comm;

        $i = 0;
        $readexcel = $his_comm->phpreadExcel();
        foreach ($readexcel['excel_array'] as $key => $value) {
            $data['shop_price'] = $value[1];
            $res = M('goods')->where('goods_sn',$value[0])->save($data);
            if ($res) {
                # code...
                $i++;
            }
        };
        echo $i;
        unlink($readexcel['file_path']);
    }

    /*
     *更新商品
     */
    public function updata_goods(){
        $his_comm = new His_comm;

        $i = 0;
        $readexcel = $his_comm->phpreadExcel();
        // var_dump($readexcel);exit;
        foreach ($readexcel['excel_array'] as $key => $value) {
            $data['goods_sn'] = $value[0];
            $data['goods_name'] = $value[1];
            $data['goods_gg'] = $value[2];
            $data['goods_xh'] = $value[3];
            $data['goods_dw'] = $value[4];
            $data['goods_cd'] = $value[7];
            $data['goods_pym'] = $value[5];
            $data['goods_zczh'] = $value[8];
            $data['cat_id'] = $value[10];
            $data['suppliers_id'] = $value[11];
            $data['brand'] = $value[9];
            $data['store_count'] = 10000;
            $res = M('goods')->add($data);
            if ($res) {
                $i++;
            }
        };
        unlink($readexcel['file_path']);
    }
}
